package com.javabi.boardgame.game.draughts;

import java.util.Arrays;
import java.util.List;

import com.javabi.boardgame.game.draughts.piece.DraughtsPiece;
import com.javabi.boardgame.model.IBoardGame;
import com.javabi.boardgame.model.IPlayer;
import com.javabi.boardgame.model.board.IBoard;
import com.javabi.boardgame.model.board.grid.GridLocation;
import com.javabi.boardgame.model.status.IStatus;
import com.javabi.boardgame.model.status.StatusMap;

public class DraughtsGame implements IBoardGame {

	private final DraughtsBoard board = new DraughtsBoard();
	private final StatusMap<DraughtsStatus> statusMap = new StatusMap<DraughtsStatus>();

	public DraughtsGame() {

		// Setup players
		setupPlayer(DraughtsPlayer.WHITE);
		setupPlayer(DraughtsPlayer.BLACK);
	}

	private void setupPlayer(DraughtsPlayer player) {
		DraughtsStatus status = new DraughtsStatus(player);
		newPieces(player.getColor(), status);
		statusMap.put(player, status);
	}

	private void setupBlackPlayer() {
		DraughtsPlayer player = DraughtsPlayer.BLACK;
		DraughtsStatus status = new DraughtsStatus(player);
		newPieces(player.getColor(), status);
		statusMap.put(player, status);
	}

	private void newPieces(DraughtsColor color, DraughtsStatus status) {
		// TODO
	}

	private void newPiece(DraughtsPiece piece, DraughtsStatus status, char xChar, int y) {
		int x = GridLocation.convert(xChar, 8);
		newPiece(piece, status, x, y);
	}

	private void newPiece(DraughtsPiece piece, DraughtsStatus status, int x, int y) {
		GridLocation location = new GridLocation(x, y);
		board.setPiece(location, piece);
		status.addPiece(piece);
	}

	@Override
	public String getName() {
		throw new UnsupportedOperationException();
	}

	@Override
	public List<? extends IPlayer> getPlayerList() {
		return Arrays.asList(DraughtsPlayer.WHITE, DraughtsPlayer.BLACK);
	}

	@Override
	public IBoard<?, ?> getBoard() {
		return board;
	}

	@Override
	public IStatus getStatus(IPlayer player) {
		return statusMap.get(player);
	}

}
